package com.xuxin.nantai.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.glide.GlideApp;

import butterknife.BindView;
import io.reactivex.rxjava3.disposables.Disposable;


public class SplashActivity extends BaseActivity {
    private RequestOptions myOptions = new RequestOptions().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL);
    @BindView(R.id.splash_image)
    ImageView splash_image;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    public void initView() {
        GlideApp.with(this).load(R.mipmap.img_splash).apply(myOptions).into(splash_image);
    }

    @Override
    public void initData() {
        rxPermissions();
    }

    @Override
    public void onComplete() {

    }


    @Override
    public void onClick(View view) {
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == 0) {
                ActivityUtils.startActivity(LoginActivity.class);
                finish();
            }
            return false;
        }
    });

    /**
     * 连环嵌套申请权限
     */
    private Disposable disposable;

    private void rxPermissions() {
        final RxPermissions rxPermissions = new RxPermissions(this);
        //读取手机状态
        disposable = rxPermissions.request(BaseConstant.permission_phone).subscribe(granted_phone -> {
            if (granted_phone) {
                //相机权限
                disposable = rxPermissions.request(BaseConstant.permission_camera).subscribe(granted_camera -> {
                    if (granted_camera) {
                        //储存权限
                        disposable = rxPermissions.request(BaseConstant.permission_storage).subscribe(granted_storage -> {
                            if (granted_storage) {
                                mHandler.sendEmptyMessageDelayed(0, 1000);
                            } else {
                                ToastUtils.showShort("权限已被拒绝");
                                finish();
                            }
                        });
                    } else {
                        ToastUtils.showShort("权限已被拒绝");
                        finish();
                    }
                });
            } else {
                ToastUtils.showShort("权限已被拒绝");
                finish();
            }
        });
    }
}